package com.eascs.web.p2p.action;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.p2p.common.entity.DatadicEntity;
import com.eascs.p2p.common.entity.DatadicItemEntity;
import com.eascs.p2p.common.service.IDatadicService;
import com.eascs.p2p.common.vo.DatadicVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.MessageRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;

@RestController
@RequestMapping("/datadicRest")
public class DatadicRestController extends BaseRestController {
	@Reference
	IDatadicService datadicService;

	/**
	 * @Title: to_eidt
	 * @Description: 字典编辑页面
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2016年8月5日 下午3:47:51
	 */
	@RequestMapping(value = "/to_edit_datadic")
	public HtmlRestResult to_eidt(String id) {
		ModelResult<DatadicEntity> datadicModel = datadicService.findDatadicByID(id);
		DatadicEntity vo = new DatadicEntity();
		if (datadicModel.isSuccess()) {
			vo = datadicModel.getModel();
		}

		ViewContext.put("vo", vo);
		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/datadic/datadic_edit");

	}
	
	/**
	 * @Title: to_save
	 * @Description: 字典新增页面
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2016年8月5日 下午3:49:26
	 */
	@RequestMapping(value = "/to_save_datadic")
	public HtmlRestResult to_save(String id) {
		ModelResult<DatadicEntity> datadicModel = datadicService.findDatadicByID(id);
		DatadicEntity vo = new DatadicEntity();
		if (datadicModel.isSuccess()) {
			vo = datadicModel.getModel();
		}

		ViewContext.put("vo", vo);
		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/datadic/datadic_edit");

	}
	
	
	@RequestMapping(value = "/datadic_item")
	public HtmlRestResult datadic_item(DatadicVO vo) {

		vo.setPageSize(10);
		ModelResult<PageResult<Object[]>> model = datadicService.queryDatadicItemByID(vo);
		PageResult<Object[]> pageResult = new PageResult<Object[]>();
		if (model.isSuccess()) {
			pageResult = model.getModel();
		}
		String[] titles = { "数据代码", "数据名称","数据描述","排序"};
		AjaxButton button2=new AjaxButton("修改                   ", "/datadicRest/to_edit_datadic_item?id=");
		AjaxButton button3=new AjaxButton("删除                   ", "/datadicRest/delete_datadic_item?id=");
		
		Button[] detailButton = new Button[] { button2, button3};
		String pageUrl = "/datadicRest/datadic_item";
		pageUrl = UrlUtil.appendUrl(pageUrl, "name", vo.getName());
		pageUrl = UrlUtil.appendUrl(pageUrl, "code", vo.getCode());
		pageUrl = UrlUtil.appendUrl(pageUrl, "showPage", "");
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		paginationInfo.setBuildPagingNavigation(true);
		// paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);

		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/datadic/datadic_item");
	}

	
	
	/**
	 * @Title: to_edit_datadic_item
	 * @Description: 字典详情编辑页面
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2016年8月8日 下午1:20:05
	 */
	@RequestMapping(value = "/to_edit_datadic_item")
	public HtmlRestResult to_edit_datadic_item(String id) {
		ModelResult<DatadicItemEntity> datadicModel = datadicService.findDatadicItemByID(id);
		DatadicItemEntity vo = new DatadicItemEntity();
		if (datadicModel.isSuccess()) {
			vo = datadicModel.getModel();
		}

		ViewContext.put("vo", vo);

		ViewContext.put("ddNo", vo.getDdNo());
		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/datadic/datadic_item_edit");

	}
	
	
	/**
	 * @Title: to_save_datadic_item
	 * @Description: 字典详情增加页面
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return HtmlRestResult
	 * @throws
	 * @date 2016年8月8日 下午1:20:27
	 */
	@RequestMapping(value = "/to_save_datadic_item")
	public HtmlRestResult to_save_datadic_item(String id) {
		
		ViewContext.put("ddNo", id);
		
		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/datadic/datadic_item_edit");

	}
	
	/**
	 * @Title: editDatadic
	 * @Description: 添加或者修改字典信息
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return BaseRestResult<String>
	 * @throws
	 * @date 2016年8月5日 下午3:49:51
	 */
	@RequestMapping(value = "/edit_datadic")
	public BaseRestResult<String> editDatadic(DatadicEntity vo) {
		BaseRestResult<String> result = null;

		if (vo.getId() == null) { // 新增
			ModelResult<Integer> numModel=datadicService.isExistDatadic(vo.getDdCode());
			int num=0;
			if(numModel.isSuccess()){
				num=numModel.getModel();
			}
			if(num>0){
				return BaseRestResult.warn("字典项code不能重复");
			}
			ModelResult<String> model = datadicService.addDatadic(vo);
			if (model.isSuccess()) {
				if (StringUtil.isNotBlank(model.getModel())) {
					result = BaseRestResult.info("添加成功").andRefresh();

				} else {
					result = BaseRestResult.error("添加失败");
				}

			} else {

				result = BaseRestResult.error("添加失败,数据库发生异常");
			}

		} else {
			ModelResult <DatadicEntity> datadicModel=datadicService.findDatadicByID(vo.getId());
			DatadicEntity entity=new DatadicEntity();
			if(datadicModel.isSuccess()){
				entity=datadicModel.getModel();
			}
			
			entity.setDdName(vo.getDdName());
			entity.setDdDesc(vo.getDdDesc());
			ModelResult<Boolean> model = datadicService.updateDatadic(entity);
			if (model.isSuccess()) {
				if (model.getModel()) {
					result = BaseRestResult.info("添加成功").andRefresh();

				} else {
					result = BaseRestResult.error("修改失败");
				}

			} else {

				result = BaseRestResult.error("修改失败,数据库发生异常");
			}

		}

		return result;
	}

	/**
	 * @Title: delete_datadic
	 * @Description: 删除字典信息
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return BaseRestResult<String>
	 * @throws
	 * @date 2016年8月5日 下午3:50:09
	 */
	@RequestMapping(value = "/delete_datadic")
	public BaseRestResult<String> delete_datadic(String id) {

		ModelResult<Boolean> model = datadicService.deleteDatadic(id);
		if (model.isSuccess()) {
			if (model.getModel()) {
				BaseRestResult.info("操作成功");
				return BaseRestResult.refresh();
			} else {
				return BaseRestResult.warn("操作失败");
			}
		} else {
			return BaseRestResult.error("数据库操作失败");
		}

	}
	
	
	
	/**
	 * @Title: delete_datadic_item
	 * @Description: 删除字典详情信息
	 * @author 张庆奇
	 * @param id
	 * @return
	 * @return BaseRestResult<String>
	 * @throws
	 * @date 2016年8月8日 下午1:21:16
	 */
	@RequestMapping(value = "/delete_datadic_item")
	public BaseRestResult<String> delete_datadic_item(String id) {

		ModelResult<Boolean> model = datadicService.deleteDatadicItem(id);
		if (model.isSuccess()) {
			if (model.getModel()) {
				BaseRestResult.info("操作成功");
				return BaseRestResult.refresh();
			} else {
				return BaseRestResult.warn("操作失败");
			}
		} else {
			return BaseRestResult.error("数据库操作失败");
		}

	}
	
	
	@RequestMapping(value = "/edit_datadic_item")
	public BaseRestResult<String> editDatadicItem(DatadicItemEntity vo) {
		BaseRestResult<String> result = null;

		if (vo.getId() == null) { // 新增
			ModelResult<Integer> numModel=datadicService.isExistDatadicItem(vo.getDdNo(),vo.getItemCode());
			int num=0;
			if(numModel.isSuccess()){
				num=numModel.getModel();
			}
			if(num>0){
				return BaseRestResult.warn("字典项code不能重复");
			}
			ModelResult<String> model = datadicService.addDatadicItem(vo);
			if (model.isSuccess()) {
				if (StringUtil.isNotBlank(model.getModel())) {
					result = BaseRestResult.info("添加成功").andRefresh();

				} else {
					result = BaseRestResult.error("添加失败");
				}

			} else {

				result = BaseRestResult.error("添加失败,数据库发生异常");
			}

		} else {
			ModelResult <DatadicItemEntity> datadicModel=datadicService.findDatadicItemByID(vo.getId());
			DatadicItemEntity entity=new DatadicItemEntity();
			if(datadicModel.isSuccess()){
				entity=datadicModel.getModel();
			}
			
			entity.setItemName(vo.getItemName());
			entity.setItemDesc(vo.getItemDesc());
			ModelResult<Boolean> model = datadicService.updateDatadicItem(entity);
			if (model.isSuccess()) {
				if (model.getModel()) {
					result = BaseRestResult.info("添加成功").andRefresh();

				} else {
					result = BaseRestResult.error("修改失败");
				}

			} else {

				result = BaseRestResult.error("修改失败,数据库发生异常");
			}

		}

		return result;
	}


}
